package com.organization

import grails.test.*

class DataServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testDataServiceMethods() {
        //create testing service
        def testService = new DataService()

        //mock commonService methods
        def mockControl = mockFor(CommonService)
        testService.commonService = mockControl.createMock()

        //mock department,project and employee services
        def mockControlDepService = mockFor(DepartmentService)
        mockControlDepService.demand.getDepartmentInfo(1..1) {nodeId->
            testService.commonService.goodResponse
        }
        mockControlDepService.demand.updateDepartmentInfo(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlDepService.demand.addDepartment(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlDepService.demand.deleteDepartment(1..1) {params->
            testService.commonService.goodResponse
        }
        testService.departmentService = mockControlDepService.createMock()

        def mockControlProjService = mockFor(ProjectService)
        mockControlProjService.demand.getProjectInfo(1..1) {nodeId->
            testService.commonService.goodResponse
        }
        mockControlProjService.demand.updateProjectInfo(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlProjService.demand.addProject(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlProjService.demand.deleteProject(1..1) {params->
            testService.commonService.goodResponse
        }
        testService.projectService = mockControlProjService.createMock()

        def mockControlEmplService = mockFor(EmployeeService)
        mockControlEmplService.demand.getEmployeeInfo(1..1) {nodeId->
            testService.commonService.goodResponse
        }
        mockControlEmplService.demand.updateEmployeeInfo(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlEmplService.demand.addEmployee(1..1) {params->
            testService.commonService.goodResponse
        }
        mockControlEmplService.demand.deleteEmployee(1..1) {params->
            testService.commonService.goodResponse
        }
        testService.employeeService = mockControlEmplService.createMock()

        //test getOrganizationInfo()
        def result = testService.getOrganizationInfo()
        assertNotNull "null result in getOrganizationInfo",result
        assertNotNull "organization name field is null",result.name
        assertEquals "true",result.success

        //test getElement(nodeId)
        result = testService.getElement("Undefined_123")
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.getElement("Organization")
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        assertEquals testService.commonService.rootNodeStr,result.name
        result = testService.getElement("Department_1")
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.getElement("Project_1")
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.getElement("Employee_1")
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success

        //test updateElement(params)
        result = testService.updateElement([nodeId:"Undefined_123"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.updateElement([nodeId:"Organization"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.updateElement([nodeId:"Department_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.updateElement([nodeId:"Project_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.updateElement([nodeId:"Employee_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success

        //test addElement(params)
        result = testService.addElement([parentNodeId:"Undefined_123"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.addElement([parentNodeId:"Organization"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.addElement([parentNodeId:"Department_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.addElement([parentNodeId:"Project_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.addElement([parentNodeId:"Employee_1"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success

        //test deleteElement(params)
        result = testService.deleteElement([nodeId:"Undefined_123"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.deleteElement([nodeId:"Organization"])
        assertNotNull "getElement result is null",result
        assertEquals "false",result.success
        result = testService.deleteElement([nodeId:"Department_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.deleteElement([nodeId:"Project_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success
        result = testService.deleteElement([nodeId:"Employee_1"])
        assertNotNull "getElement result is null",result
        assertEquals "true",result.success

        //verify count of mocked methods calls
        mockControl.verify()
     }
}
